package org.chunchu.todo;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Provider;

import org.chunchu.todo.model.Category;
import org.chunchu.todo.mvp.impl.CategoryList;
import org.chunchu.todo.mvp.impl.CategoryListPresenter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class CategoryListAdapter extends BaseAdapter implements CategoryList {

	private static final String TAG_POS = "pos";

	private CategoryListPresenter presenter;

	@Inject
	private static Provider<Context> contextProvider;

	private LayoutInflater layoutInflater;

	private List<Category> categories;

	@Inject
	public CategoryListAdapter(CategoryListPresenter presenter) {
		layoutInflater = LayoutInflater.from(contextProvider.get());
		this.presenter = presenter;
		this.presenter.initializeWith(this);
	}

	@Override
	public List<Category> getCategories() {
		return categories;
	}

	@Override
	public void setCategories(List<Category> categories) {
		this.categories = categories;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return categories.size();
	}

	@Override
	public Object getItem(int position) {
		return categories.get(position);
	}

	@Override
	public long getItemId(int position) {
		return categories.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup viewGroup) {
		Bundle posBundle = new Bundle();
		posBundle.putInt(TAG_POS, position);
		convertView = layoutInflater.inflate(R.layout.category_item, null);

		TextView tvCategoryName = (TextView) convertView
				.findViewById(R.id.name);
		Button btnEditCategory = (Button) convertView
				.findViewById(R.id.editCategoryItem);
		Button btnRemoveCategory = (Button) convertView
				.findViewById(R.id.removeCategoryItem);

		tvCategoryName.setOnClickListener(clickListerner);
		tvCategoryName.setTag(posBundle);
		btnEditCategory.setTag(posBundle);
		btnRemoveCategory.setTag(posBundle);

		Category category = categories.get(position);

		if (category.getId() == 1) {
			btnEditCategory.setEnabled(false);
			btnRemoveCategory.setEnabled(false);
			btnEditCategory.setVisibility(View.INVISIBLE);
			btnRemoveCategory.setVisibility(View.INVISIBLE);
		} else {
			btnEditCategory.setOnClickListener(editListerner);
			btnRemoveCategory.setOnClickListener(removeListerner);
		}

		tvCategoryName.setText(category.getName());
		return convertView;
	}

	private final OnClickListener clickListerner = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Bundle bundle = (Bundle) v.getTag();
			Integer position = bundle.getInt(TAG_POS);
			presenter.changePosition(position);
		}
	};

	private final OnClickListener removeListerner = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Bundle bundle = (Bundle) v.getTag();
			Integer position = bundle.getInt(TAG_POS);
			presenter.removeItem(position);
		}
	};

	private final OnClickListener editListerner = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Bundle bundle = (Bundle) v.getTag();
			Integer position = bundle.getInt(TAG_POS);
			presenter.editItem(position);
		}
	};

	@Override
	public Category getCategoryAt(int position) {
		return categories.get(position);
	}

	@Override
	public void updateUi() {
		notifyDataSetChanged();
	}
}
